//
//  UITextFiled+Extension.swift
//  NewRegProject
//
//  Created by hlt on 2023/2/15.
//

import Foundation
import UIKit

extension UITextField{
    private static var color:UIColor?
    
    private static var textfont:UIFont?
    
    var placeHolderTextFont:UIFont{
        set{
            if(self.attributedPlaceholder == nil){
                let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: self.placeholder ?? "")
                attributedString.addAttribute(.font, value: newValue, range: NSRange(location: 0, length: attributedString.length))
                self.attributedPlaceholder = attributedString
            }else{
                let attributedString:NSMutableAttributedString = NSMutableAttributedString(attributedString: self.attributedPlaceholder!)
                attributedString.addAttribute(.font, value: newValue, range: NSRange(location: 0, length: attributedString.length))
                self.attributedPlaceholder = attributedString
            }
            Self.textfont = newValue
        }
        get{
    
            return Self.textfont!
        }
    }
    
    
    var placeHolderTextColor:UIColor{
        set{
            Self.color = newValue
            
            if(self.attributedPlaceholder == nil){
                let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: self.placeholder ?? "")
                attributedString.addAttribute(.font, value: Self.textfont!, range: NSRange(location: 0, length: attributedString.length))
                self.attributedPlaceholder = attributedString
            }else{
                let attributedString:NSMutableAttributedString = NSMutableAttributedString(attributedString: self.attributedPlaceholder!)
                attributedString.addAttribute(.foregroundColor, value: Self.color!, range: NSRange(location: 0, length: attributedString.length))
                self.attributedPlaceholder = attributedString
            }
        }
        get{
     
            return Self.color!
        }
    }

  @objc  func placeHolderTextColor(color:UIColor,font:UIFont) {
        
        let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: self.placeholder ?? "")
        
        attributedString.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: attributedString.length))
        attributedString.addAttribute(.font, value: font, range: NSRange(location: 0, length: attributedString.length))
        self.attributedPlaceholder = attributedString
    }
    
    convenience init(_ text:String = "",_ textColor:UIColor?, _ font:UIFont?,_ textPlaceHolder:String ,_ textPlaceHoldercolor:UIColor,_ textPlaceHolderFont:UIFont) {
        self.init()
        self.text = text
        self.textColor = textColor
        self.font = font
        self.placeholder = textPlaceHolder
        self.placeHolderTextFont = textPlaceHolderFont
        self.placeHolderTextColor = textPlaceHoldercolor
    }
}

extension UITextView{
    func loadHTMLTextDocument(text:String){
        let str = text.replacingOccurrences(of: "height:", with: "h")
        let adaptString = "<head><style>img{max-width:\(KScreenWidth) !important;height:auto}</style></head>\(str)"
        let data = adaptString.data(using: .unicode)!
        self.isUserInteractionEnabled = false
        let attributedString:NSMutableAttributedString = try! NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html], documentAttributes: nil)
        self.attributedText = attributedString
    }

}

